package org.wk.account.api.controller;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.wk.account.enums.AccountType;
import org.wk.account.enums.ResponseCode;
import org.wk.account.service.*;
import org.wk.core.common.tool.HashUtils;
import org.wk.core.common.view.ResponseVo;
import org.wk.core.db.tool.WebUtils;
import org.wk.user.domain.User;
import org.wk.user.service.UserService;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName AccountsController
 * @Description 账户管理
 * @Author ZhaoPing'an
 * @Date 2018/5/17 10:29
 */
@RestController
public class SubAccountController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SubAccountService accountService;

    @Autowired
    private SubAccountZaiService zaiService;

    @Autowired
    private SubAccountBaoService baoService;

    @Autowired
    private SubAccountZhongService zhongService;

    @Autowired
    private SubAccountLingService lingService;

    @Autowired
    private SubAccountZuanService zuanService;

    @Autowired
    private UserService userService;

    /**
     * @Description 获取账户列表
     * @author ZhaoPing'an
     * @Date 2018/5/17 10:36
     */
    @RequestMapping(value="/accounts",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String findAllAccount() {
        logger.info("获取我的账户=====>>>{}");
        ResponseVo responseVo = accountService.selectSubAccount();
        logger.info("获取我的账户返回值=====>>>{}",responseVo.toString());
        return responseVo.toString();

    }

    /**
     * 各个子账号的账户详情
     * @author TangShenShen
     * @date 2018/5/29 11:47
     */
    @RequestMapping(value="/accounts/{account_type}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String findSubAccoutDetail(@PathVariable("account_type") String accountType){
        logger.info("当前用户的账户详情>>>>{}",accountType);
        ResponseVo responseVo = accountService.findSubAccoutDetail(accountType);
        String resStr = responseVo.toString();
        logger.info("当前用户的账户详情返回值>>>>{}", resStr);
        return resStr;
    }

    /**
     * @Description 兑换操作
     * @author ZhaoPing'an
     * @Date 2018/5/17 16:00
     */
    @RequestMapping(value = "/accounts/{source_account_type}/exchange/{target_account_type}",
            method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String exchange(@PathVariable("source_account_type") String sourceAccountType,
                            @PathVariable("target_account_type") String targetAccountType,
                            @RequestBody JSONObject jsonObject) {
        ResponseVo result = new ResponseVo();
        // 校验二级密码
        Map<String,String> res = validateTwoLevelPassword(jsonObject);
        if (res.containsKey("flag")){
            return ResponseVo.success(res.get("code"),res.get("message")).toString();
        }
        if (sourceAccountType.equals(AccountType.JING_ZAI.getCode())){
            logger.info("鲸仔兑换操作=====>>>{}");
            result = zaiService.jingzaiExchangeOthers(targetAccountType,jsonObject);
        }
        if (sourceAccountType.equals(AccountType.JING_BAO.getCode())){
            logger.info("鲸宝兑换操作=====>>>{}");
            result = baoService.jingbaoExchangeOthers(targetAccountType,jsonObject);
        }
        logger.info("兑换结果=====>>>{}", result.toString());
        return result.toString();

    }

    /**
     * 子账号的变动记录
     * @author TangShenShen
     * @date 2018/5/29 11:48
     */
    @RequestMapping(value="/accounts/{account_type}/seq",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseVo findSubAccoutSeq(@PathVariable("account_type") String accountType,
                                       @RequestParam(required = false,defaultValue = "1") Integer page_num, @RequestParam(required = false,defaultValue = "15") Integer page_size){
        logger.info("当前用户的账户变动列表>>>>{}",accountType);
        ResponseVo responseVo = accountService.findSubAccountSeqs(accountType,page_num,page_size);
        logger.info("当前用户的账户变动列表返回值>>>>{}",responseVo.toString());
        return responseVo;
    }

    /**
     * 鲸仔转赠他人
     * @author TangShenShen
     * @date 2018/5/29 11:49
     */
    @RequestMapping(value="/accounts/jing_zai/presentothers",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseVo presentOthers(@RequestBody JSONObject jsonObject){
        logger.info("鲸仔转赠>>>>{}");
        // 校验二级密码
        Map<String,String> result = validateTwoLevelPassword(jsonObject);
        if (result.containsKey("flag")){
            return ResponseVo.success(result.get("code"),result.get("message"));
        }
        ResponseVo responseVo = zaiService.presentOthers(jsonObject);
        logger.info("鲸仔转赠返回值>>>>{}",responseVo.toString());
        return responseVo;
    }

    /**
     * @Description 鲸种激活別人
     * @Param [jsonObject]
     * @author ZhaoPing'an
     * @Date 2018/5/26 11:59
     */
    @RequestMapping(value="/accounts/jing_zhong/activateothers",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseVo activateOthers(@RequestBody JSONObject jsonObject){
        // 校验二级密码
        Map<String,String> result = validateTwoLevelPassword(jsonObject);
        if (result.containsKey("flag")){
            return ResponseVo.success(result.get("code"),result.get("message"));
        }
        logger.info("激活別人>>>>{}");
        ResponseVo responseVo = zhongService.activateOthers(jsonObject);
        logger.info("激活別人返回值>>>>{}",responseVo.toString());
        return responseVo;
    }

    /**
     * @Description 鲸种激活自己
     * @Param [jsonObject]
     * @author ZhaoPing'an
     * @Date 2018/5/26 12:00
     */
    @RequestMapping(value="/accounts/jing_zhong/activateoneself",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseVo activateOneself(@RequestBody JSONObject jsonObject){
        // 校验二级密码
        Map<String,String> result = validateTwoLevelPassword(jsonObject);
        if (result.containsKey("flag")){
            return ResponseVo.success(result.get("code"),result.get("message"));
        }
        logger.info("激活自己>>>>{}");
        ResponseVo responseVo = zhongService.activateOneself(jsonObject);
        logger.info("激活自己返回值>>>>{}",responseVo.toString());
        return responseVo;
    }

    /**
     * @Description 鲸灵提现服务
     * @param jsonObject
     * @author ZhaoPing'an
     * @Date 2018/6/27 15:27
     */
    /*@RequestMapping(value="/accounts/jing_ling/exchangecny",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String lingExchangecny(@RequestBody JSONObject jsonObject){
        ResponseVo result = new ResponseVo();
        logger.info("鲸灵提取现金=====>>>{}",jsonObject.toString());
        result = lingService.lingExchangeCNY(jsonObject);
        logger.info("鲸灵提取现金结果=====>>>{}", result.toString());
        return result.toString();
    }

    *//**
     * @Description 鲸钻提现
     * @param jsonObject
     * @author ZhaoPing'an
     * @Date 2018/6/28 11:52
     *//*
    @RequestMapping(value="/accounts/jing_zuan/exchangecny",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String zuanExchangecny(@RequestBody JSONObject jsonObject){
        ResponseVo result = new ResponseVo();
        logger.info("鲸钻提取现金=====>>>{}",jsonObject.toString());
        result = zuanService.zuanExchangeCNY(jsonObject);
        logger.info("鲸钻提取现金结果=====>>>{}", result.toString());
        return result.toString();
    }*/

    /*
     * @description 转增、激活、兑换操作时候密码校验
     * @param
     * @author ZhaoPing'an
     * @date 2018/7/31 17:26
     */
    private Map<String,String> validateTwoLevelPassword(JSONObject jsonObject){
        Map<String,String> result = new HashMap<String, String>();
        String password = jsonObject.getString("password");
        if (StringUtils.isEmpty(password)){
            result.put("flag","no");
            result.put("code",ResponseCode.TWO_LEVEL_PASSWORD_ERROR.getCode());
            result.put("message",ResponseCode.TWO_LEVEL_PASSWORD_ERROR.getMessage());
            return result;
        }
        User user = userService.findUserInfo(WebUtils.getUser().getId());
        if (StringUtils.isEmpty(user.getTwoLevelPassword())){
            result.put("flag","no");
            result.put("code",ResponseCode.NO_TWO_LEVEL_PASSWORD.getCode());
            result.put("message",ResponseCode.NO_TWO_LEVEL_PASSWORD.getMessage());
            return result;
        }
        if (!user.getTwoLevelPassword().equals(HashUtils.getSHA256Str(password))){
            result.put("flag","no");
            result.put("code",ResponseCode.TWO_LEVEL_PASSWORD_ERROR.getCode());
            result.put("message",ResponseCode.TWO_LEVEL_PASSWORD_ERROR.getMessage());
            return result;
        }
        return result;
    }

}
